package com.icebartech.base.modules.user.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.icebartech.base.modules.user.dto.AppPermissionInfoDTO;
import com.icebartech.base.modules.user.mapper.AppPermissionMapper;
import com.icebartech.base.modules.user.model.AdminUserCore;
import com.icebartech.base.modules.user.model.AppMenu;
import com.icebartech.base.modules.user.model.AppPermission;
import com.icebartech.base.modules.user.model.AppRole;
import com.icebartech.base.modules.user.service.AdminUserCoreService;
import com.icebartech.base.modules.user.service.AppMenuService;
import com.icebartech.base.modules.user.service.AppPermissionService;
import com.icebartech.base.modules.user.service.AppRolePermissionService;
import com.icebartech.base.modules.user.service.AppRoleService;
import com.icebartech.core.modules.AbstractService;
import com.icebartech.core.modules.BaseMapper;
import com.icebartech.core.utils.ExtBeanUtils;

@Service
public class AppPermissionServiceImpl extends AbstractService implements AppPermissionService {
    
    private static Logger logger = LoggerFactory.getLogger(AppPermissionServiceImpl.class);
    
    @Autowired
    private AppPermissionMapper appPermissionMapper;
    
    @Autowired
    private AppMenuService appMenuService;
    
    @Autowired
    private AdminUserCoreService adminUserCoreService;
    
    @Autowired
    private AppRoleService appRoleService;
    
    @Autowired
    private AppRolePermissionService appRolePermissionService;
    
    @Override
    public BaseMapper getBaseMapper() {
        return this.appPermissionMapper;
    }
    
    @Override
    public List<AppPermissionInfoDTO> getAllAppPermissions() {
        return getAppPermissionByParentId(0L, null);
    }
    
    @Override
    public List<AppPermissionInfoDTO> findUserPermissions(Long userId) {
    	AdminUserCore userCore = adminUserCoreService.findById(userId);
    	if(userCore == null){
    		return null;
    	}
    	List<AppPermissionInfoDTO> dataList = new ArrayList<>();
    	String roleIdStr = userCore.getAppRoleId();
    	if(StringUtils.isNotEmpty(roleIdStr)){
    		Long roleId = Long.valueOf(roleIdStr);
    		AppRole appRole = appRoleService.findById(roleId);
    		if(appRole != null){
    			String roleKey = appRole.getRoleKey();
    			List<Long> permissionIds = appRolePermissionService.findPermissionsByRoleKey(roleKey);
    			dataList = getAppPermissionByParentId(0L, permissionIds);
    		}
    	}
    	return dataList;
    }
    
    
    public List<AppPermissionInfoDTO> getAppPermissionByParentId(Long parentId, List<Long> permissionIds) {
        List<AppPermissionInfoDTO> appPermissionInfos = new ArrayList<>();
        List<AppPermission> appPermissions = appPermissionMapper.getAppPermissionByParentId(parentId, "page", permissionIds);
        if(CollectionUtils.isEmpty(appPermissions)){
        	return appPermissionInfos;
        }
        for (AppPermission appPermission : appPermissions) {
            try {
                AppPermissionInfoDTO appPermissionInfo = assemblePermissionInfo(appPermission, permissionIds);
                appPermissionInfos.add(appPermissionInfo);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                continue;
            }
        }
        return appPermissionInfos;
    }
    
    private AppPermissionInfoDTO assemblePermissionInfo(AppPermission appPermission, List<Long> permissionIds){
    	AppPermissionInfoDTO appPermissionInfo = new AppPermissionInfoDTO();
    	Long permissionId = appPermission.getId();
    	try{
    		ExtBeanUtils.copyProperties(appPermissionInfo, appPermission);
    	}catch(Exception e){
    		return null;
    	}
        //设置菜单名称,url等字段
        String permissionType = appPermission.getPermissionType();
        if("page".equals(permissionType)){
        	Long menuId = Long.valueOf(appPermission.getResourceId());
        	AppMenu appMenu = appMenuService.findById(menuId);
        	if(appMenu != null){
        		appPermissionInfo.setMenuLevel(appMenu.getMenuLevel());
        		appPermissionInfo.setMenuName(appMenu.getMenuName());
        		appPermissionInfo.setMenuUrl(appMenu.getMenuUrl());
        	}
        }else{
        	//其他比如button按钮等的权限处理
        }
        appPermissionInfo.setChildPermissions(getAppPermissionByParentId(permissionId, permissionIds));
        return appPermissionInfo;
    }
    
}
